package https.callback;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.example.xuegengwang.xuegengwang.R;
import com.example.xuegengwang.xuegengwang.wxapi.MyWxApi;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;


import common.Constant;
import guidePage.LoginPage;
import utils.MyLogUtils;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/2/27.
 * 普通网络访问回调
 */

public class BaseCommonCallback implements Callback.CommonCallback<String> {
    private static final String TAG = "BaseCommonCallback";
    public static boolean stop = false;//是否继续解析
    private Context context;

    public BaseCommonCallback(Context context) {
        this.context = context;
    }

    @Override
    public void onSuccess(String result) {
        try {
            JSONObject jsonObject = new JSONObject(result);
            int errorCode = jsonObject.getInt("err_code");
            String alert = jsonObject.getString("alert");//是否要展示提示信息
            if (alert != null) {
                String msg = jsonObject.getString("msg");//提示信息
                if (Constant.ALERT_Y.equals(alert)) {//需要展示信息
                    ToastUtils.shortToast(msg);
                }
            }
            MyLogUtils.e(TAG, "onSuccess: " + errorCode);
            switch (errorCode) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                    stop = false;
                    break;
                case 10:
                    MyLogUtils.e(TAG, "onSuccess: " + jsonObject.getString("msg"));
                    //签名错误
                    stop = true;
                    break;
                case 25:
                    //用户未登录
                    Intent i = new Intent();
                    i.setClass(context, LoginPage.class);
                    ((Activity) context).startActivityForResult(i, Constant.LOGIN_REQUEST_CODE);
                    ((Activity) context).overridePendingTransition(R.anim.zoomin, 0);
                    stop = true;
                    break;
                case 27:
                    //未绑定微信
                    ToastUtils.shortToast("未绑定微信");
                    MyWxApi.Login();
                    break;
                default:
                    break;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {

    }

    @Override
    public void onCancelled(CancelledException cex) {

    }

    @Override
    public void onFinished() {

    }
}
